<!DOCTYPE HTML>
<html>

<head>
	<link rel="bookmark"  type="image/x-icon"  href="/img/logo_miccall.png"/>
	<link rel="shortcut icon" href="/img/logo_miccall.png">
	
			    <title>
    Ame雨
    </title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" />
    <link rel="stylesheet" href="/amelin-ting/css/mic_main.css" />
    <link rel="stylesheet" href="/amelin-ting/css/dropdownMenu.css" />
    <meta name="keywords" content="Ame雨" />
    
    	<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
	 
    <noscript>
        <link rel="stylesheet" href="/amelin-ting/css/noscript.css" />
    </noscript>
    <style type="text/css">
        body:before {
          content: ' ';
          position: fixed;
          top: 0;
          background: url('/amelin-ting/img/bg.jpg') center 0 no-repeat;
          right: 0;
          bottom: 0;
          left: 0;
          background-size: cover; 
        }
    </style>

			    
  
    <script type="text/x-mathjax-config">
      MathJax.Hub.Config({
        tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
        }
      });
    </script>

    <script type="text/x-mathjax-config">
      MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for (i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
        }
      });
    </script>
    <script async type="text/javascript" src="//cdn.bootcss.com/mathjax/2.7.1/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
  


    <script src="/amelin-ting/js/jquery.min.js"></script>
    <script src="/amelin-ting/js/jquery.scrollex.min.js"></script>
    <script src="/amelin-ting/js/jquery.scrolly.min.js"></script>
    <script src="/amelin-ting/js/skel.min.js"></script>
    <script src="/amelin-ting/js/util.js"></script>
    <script src="/amelin-ting/js/main.js"></script>
	
<meta name="generator" content="Hexo 5.4.0"></head>
    
		
<!-- Layouts -->



<!--  代码渲染  -->
<link rel="stylesheet" href="/css/prism_coy.css" />
<link rel="stylesheet" href="/css/typo.css" />
<!-- 文章页 -->
<body class="is-loading">
    <!-- Wrapper 外包 s-->
    <div id="wrapper" class="fade-in">
        <!-- Intro 头部显示 s -->
        <!-- Intro 头部显示 e -->
        <!-- Header 头部logo start -->
        <header id="header">
    <a href="/amelin-ting/" class="logo">Art Space</a>
</header>
        <!-- Nav 导航条 start -->
        <nav id="nav" class="special" >
            <ul class="menu links" >
			<!-- Homepage  主页  --> 
			<li >
	            <a href="/amelin-ting/" rel="nofollow">主页</a>
	        </li>
			<!-- categories_name  分类   --> 
	        
	        <li class="active">
	            <a href="#s1">分类</a>
	                    <ul class="submenu">
	                        <li>
	                        <a class="category-link" href="/amelin-ting/categories/Go/">Go</a></li><li><a class="category-link" href="/amelin-ting/categories/django/">django</a></li><li><a class="category-link" href="/amelin-ting/categories/drf/">drf</a></li><li><a class="category-link" href="/amelin-ting/categories/gin/">gin</a></li><li><a class="category-link" href="/amelin-ting/categories/go/">go</a></li><li><a class="category-link" href="/amelin-ting/categories/linux/">linux</a></li><li><a class="category-link" href="/amelin-ting/categories/mysql/">mysql</a></li><li><a class="category-link" href="/amelin-ting/categories/python/">python</a></li><li><a class="category-link" href="/amelin-ting/categories/%E5%86%85%E7%BD%91%E7%A9%BF%E9%80%8F/">内网穿透</a></li><li><a class="category-link" href="/amelin-ting/categories/%E5%89%8D%E7%AB%AF/">前端</a>
	                    </ul>
	        </li>
	        
	        <!-- archives  归档   --> 
	        
	        
		        <!-- Pages 自定义   -->
		        
		        <li>
		            <a href="/about/" title="简历">
		                简历
		            </a>
		        </li>
		        
		        <li>
		            <a href="/group/" title="团队">
		                团队
		            </a>
		        </li>
		        
		        <li>
		            <a href="/gallery/" title="图库">
		                图库
		            </a>
		        </li>
		        
		        <li>
		            <a href="/tag/" title="标签">
		                标签
		            </a>
		        </li>
		        


            </ul>
            <!-- icons 图标   -->
			<ul class="icons">
                    
                    <li>
                        <a title="github" href="https://github.com/miccall" target="_blank" rel="noopener">
                            <i class="icon fa fa-github"></i>
                        </a>
                    </li>
                    
                    <li>
                        <a title="500px" href="http://500px.com" target="_blank" rel="noopener">
                            <i class="icon fa fa-500px"></i>
                        </a>
                    </li>
                    
			</ul>
</nav>

        <div id="main" >
            <div class ="post_page_title_img" style="height: 25rem;background-image: url(https://img1.baidu.com/it/u=3245454795,2289340346&amp;fm=26&amp;fmt=auto);background-position: center; background-repeat:no-repeat; background-size:cover;-moz-background-size:cover;overflow:hidden;" >
                <a href="#" style="padding: 4rem 4rem 2rem 4rem ;"><h2 >Python基本数据类型</h2></a>
            </div>
            <!-- Post -->
            <div class="typo" style="padding: 3rem;">
                <h2 id="一、基本数据类型的内置方法"><a href="#一、基本数据类型的内置方法" class="headerlink" title="一、基本数据类型的内置方法"></a>一、基本数据类型的内置方法</h2><h3 id="1-1-整数和浮点数"><a href="#1-1-整数和浮点数" class="headerlink" title="1.1 整数和浮点数"></a>1.1 整数和浮点数</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1</span>、整数</span><br><span class="line"><span class="comment"># 整数的转换</span></span><br><span class="line">    age = <span class="string">&quot;18&quot;</span></span><br><span class="line">    <span class="built_in">print</span> = <span class="built_in">int</span>(age)</span><br><span class="line"><span class="comment"># 非整数型的是转换不了整数，比如</span></span><br><span class="line">	height = <span class="number">1.8</span></span><br><span class="line">    <span class="built_in">print</span> = <span class="built_in">int</span>(height)  <span class="comment"># 这个是错误的，带了小数点，应该转换成浮点型</span></span><br><span class="line">    name = <span class="string">&quot;Ame&quot;</span></span><br><span class="line">    <span class="built_in">print</span> = <span class="built_in">int</span>(name)  <span class="comment"># 这个是错误的，因为字符串是英文字母组成的</span></span><br><span class="line">	<span class="built_in">print</span>(<span class="built_in">int</span>(<span class="number">11.11</span>))  <span class="comment"># 11</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="built_in">int</span>(<span class="string">&quot;11.11&quot;</span>))  <span class="comment">#报错 因为里面是字符串型的浮点型 </span></span><br><span class="line"><span class="number">2</span>、浮点型</span><br><span class="line"><span class="comment"># 浮点型的装换</span></span><br><span class="line">	height = <span class="number">180.1</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="built_in">float</span>(<span class="number">180.1</span>))  <span class="comment"># 180.1</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="built_in">float</span>(<span class="string">&quot;180.1&quot;</span>))  <span class="comment"># 180.1</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="built_in">float</span>(<span class="number">180</span>))  <span class="comment"># 180.0</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="built_in">int</span>(<span class="number">11.11</span>))  <span class="comment"># 11</span></span><br></pre></td></tr></table></figure>



<h3 id="1-2-字符串"><a href="#1-2-字符串" class="headerlink" title="1.2 字符串"></a>1.2 字符串</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1</span>、字符串的查找(索引)   索引是从<span class="number">0</span>开始的</span><br><span class="line">	name = <span class="string">&quot;ameameame&quot;</span></span><br><span class="line">    <span class="built_in">print</span>(name[<span class="number">0</span>])  <span class="comment"># a</span></span><br><span class="line">    <span class="built_in">print</span>(name[-<span class="number">1</span>])  <span class="comment"># e</span></span><br><span class="line"><span class="number">2</span>、字符串的查找(切片)</span><br><span class="line">	name = <span class="string">&quot;ameameame&quot;</span></span><br><span class="line">    <span class="built_in">print</span>(name[<span class="number">0</span>:<span class="number">6</span>:<span class="number">1</span>])  <span class="comment"># ameame 1 乃是步长的意思</span></span><br><span class="line">    <span class="built_in">print</span>(name[<span class="number">0</span>:<span class="number">6</span>:<span class="number">2</span>])  <span class="comment"># aem	2 乃是步长的意思</span></span><br><span class="line">    <span class="built_in">print</span>(name[-<span class="number">1</span>:-<span class="number">6</span>:-<span class="number">1</span>])  <span class="comment">#emaem 	3 因为查找默认是从左到右，所以得加-1来从右到左的查找</span></span><br><span class="line"><span class="number">3</span>、字符串的长度</span><br><span class="line">	name = <span class="string">&quot;ameameame&quot;</span></span><br><span class="line">	<span class="built_in">print</span>(<span class="built_in">len</span>(name))  <span class="comment"># len是能弄清楚字符串的长度  name长度为9  提示 空格也算字符</span></span><br><span class="line"><span class="number">4</span>、字符串的.sprit()用法</span><br><span class="line">	<span class="comment"># .strip是去除字符串指定首尾的字符，默认不指定的时候则是去除首尾空格</span></span><br><span class="line">    name = <span class="string">&quot;      ameameame    &quot;</span></span><br><span class="line">    <span class="built_in">print</span>(name.strip())  <span class="comment"># ameameame 去除了首尾空格</span></span><br><span class="line">    name = <span class="string">&quot;%%ameameame%%&quot;</span></span><br><span class="line">    <span class="built_in">print</span>(name.strip(<span class="string">&quot;%&quot;</span>))  <span class="comment"># ameameame 去除了首尾%</span></span><br><span class="line">    name = <span class="string">&quot;%%ameame%a%m%eame%%&quot;</span></span><br><span class="line">    <span class="built_in">print</span>(name.strip(<span class="string">&quot;%&quot;</span>))  <span class="comment"># ameame%a%m%eame  只能去首尾，中间的不能去除</span></span><br><span class="line">    <span class="comment"># 如何才能去除左边的保留右边的(.lstrip)</span></span><br><span class="line">    name = <span class="string">&quot;%%ameameame%%&quot;</span></span><br><span class="line">    <span class="built_in">print</span>(name.lstrip(<span class="string">&quot;%&quot;</span>))  <span class="comment"># ameameame%%</span></span><br><span class="line">    <span class="comment"># 如何才能去除右边的保留左边的(.rstrip)</span></span><br><span class="line">    name = <span class="string">&quot;%%ameameame%%&quot;</span></span><br><span class="line">    <span class="built_in">print</span>(name.rstrip(<span class="string">&quot;%&quot;</span>))  <span class="comment"># %%ameameame</span></span><br><span class="line"><span class="number">5</span>、字符串的判断在不在字符串里面(<span class="keyword">in</span>      <span class="keyword">not</span> <span class="keyword">in</span>)</span><br><span class="line">	name = <span class="string">&quot;ameameame&quot;</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;a&quot;</span> <span class="keyword">in</span> name)  <span class="comment"># True</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;q&quot;</span> <span class="keyword">not</span> <span class="keyword">in</span> name)  <span class="comment"># False</span></span><br><span class="line"><span class="number">6</span>、字符串的.split()用法</span><br><span class="line">	<span class="built_in">str</span> = <span class="string">&quot;1!2!3!4!5!6!7!8!9&quot;</span></span><br><span class="line">    <span class="built_in">list</span> = <span class="built_in">str</span>.split(<span class="string">&quot;!&quot;</span>)</span><br><span class="line">    <span class="built_in">print</span>(<span class="built_in">list</span>)  <span class="comment"># [&#x27;1&#x27;, &#x27;2&#x27;, &#x27;3&#x27;, &#x27;4&#x27;, &#x27;5&#x27;, &#x27;6&#x27;, &#x27;7&#x27;, &#x27;8&#x27;, &#x27;9&#x27;]</span></span><br><span class="line">    <span class="comment"># 因此可以看出字符串转换成了列表，切割的&quot;！&quot;也不会被保留，所以切记，以后碰到字符串转列表就可以用.split用法。</span></span><br><span class="line">    list1 = <span class="built_in">str</span>.split(<span class="string">&quot;!&quot;</span>,maxsplit=<span class="number">2</span>) <span class="comment"># print(list)  # [&#x27;1&#x27;, &#x27;2&#x27;, &#x27;3!4!5!6!7!8!9&#x27;]</span></span><br><span class="line">    <span class="comment"># 默认.split是从左到右切割</span></span><br><span class="line">    list2 = <span class="built_in">str</span>.rsplit(<span class="string">&quot;!&quot;</span>,maxsplit=<span class="number">2</span>)  <span class="comment"># [&#x27;1!2!3!4!5!6!7&#x27;, &#x27;8&#x27;, &#x27;9&#x27;]</span></span><br><span class="line"><span class="number">7</span>、字符串的循环方法</span><br><span class="line">	name = <span class="string">&quot;ameameame&quot;</span></span><br><span class="line">	<span class="keyword">for</span> i <span class="keyword">in</span> name</span><br><span class="line">    	<span class="built_in">print</span>(i)  <span class="comment"># 依次获取字符串中单个单个的字符</span></span><br><span class="line">        </span><br><span class="line"><span class="number">8</span>、把字符串全部变成大写或者小写</span><br><span class="line">	name = <span class="string">&quot;amEaMeame&quot;</span></span><br><span class="line">    <span class="comment"># 全部转成大写</span></span><br><span class="line">    <span class="built_in">print</span>(name.upper())  <span class="comment"># &quot;AMEAMEAME&quot;</span></span><br><span class="line">    <span class="comment"># 全部转成小写</span></span><br><span class="line">    <span class="built_in">print</span>(name.lower())  <span class="comment"># &quot;ameameame&quot;</span></span><br><span class="line">    <span class="comment"># 应用于图片验证码</span></span><br><span class="line">    </span><br><span class="line"><span class="number">9</span>、字符串开头结尾</span><br><span class="line">    res = <span class="string">&#x27;Ame Yuki lalala&#x27;</span></span><br><span class="line">    <span class="comment"># 判断字符串是否以Ame开头</span></span><br><span class="line">    <span class="built_in">print</span>(res.startswith(<span class="string">&#x27;Ame&#x27;</span>))  <span class="comment"># True</span></span><br><span class="line">    <span class="comment"># 判断字符串是否以lalala结尾</span></span><br><span class="line">    <span class="built_in">print</span>(res.endswith(<span class="string">&#x27;lalala&#x27;</span>))  <span class="comment"># True</span></span><br><span class="line"> </span><br><span class="line"><span class="number">10</span>、格式化输出 <span class="built_in">format</span></span><br><span class="line">	<span class="number">2.1</span></span><br><span class="line">    res = <span class="string">&quot;班上的时常达&#123;&#125;年的练习生&#123;&#125;, 他的爱好就是&#123;&#125;,&quot;</span> \</span><br><span class="line">          <span class="string">&quot;并且在他&#123;&#125;岁的时候， 编出了鸡你太美舞蹈&quot;</span>.<span class="built_in">format</span>(<span class="number">2.5</span>, <span class="string">&quot;小明&quot;</span>, <span class="string">&quot;唱 跳 rap 篮球&quot;</span>, <span class="number">60</span>)</span><br><span class="line">    <span class="built_in">print</span>(res)</span><br><span class="line">    班上的时常达<span class="number">2.5</span>年的练习生小明, 他的爱好就是唱 跳 rap 篮球,并且在他<span class="number">60</span>岁的时候， 编出了鸡你太美舞蹈</span><br><span class="line">    </span><br><span class="line"></span><br><span class="line">    res = <span class="string">&quot;班上的时常达&#123;0&#125;&#123;0&#125;年的练习生&#123;1&#125;&#123;1&#125;, 他的爱好就是&#123;2&#125;&#123;2&#125;,&quot;</span> \</span><br><span class="line">    	  <span class="string">&quot;并且在他&#123;3&#125;&#123;3&#125;岁的时候， 编出了鸡你太美舞蹈&quot;</span>.<span class="built_in">format</span>(<span class="number">2.5</span>, <span class="string">&quot;小明&quot;</span>, <span class="string">&quot;唱 跳 rap 篮球&quot;</span>, <span class="number">60</span>)</span><br><span class="line">    <span class="built_in">print</span>(res)</span><br><span class="line">    班上的时常达<span class="number">2.52</span><span class="number">.5</span>年的练习生小明小明, 他的爱好就是唱 跳 rap 篮球唱 跳 rap 篮球,并且在他<span class="number">6060</span>岁的时候， 编出了鸡你太美舞蹈</span><br><span class="line"></span><br><span class="line">	<span class="number">2.2</span> </span><br><span class="line">	res = <span class="string">&quot;班上的时常达&#123;year&#125;年的练习生&#123;name&#125;, 他的爱好就是&#123;hobby&#125;,&quot;</span>.<span class="built_in">format</span>(name=<span class="string">&quot;小明&quot;</span>, year=<span class="number">2.5</span>, hobby=<span class="string">&quot;篮球&quot;</span>)</span><br><span class="line">	<span class="built_in">print</span>(res)</span><br><span class="line">    班上的时常达<span class="number">2.5</span>年的练习生小明, 他的爱好就是篮球,	</span><br><span class="line">    </span><br><span class="line"><span class="number">11</span>、统计字符串中字符出现的次数</span><br><span class="line">	name = <span class="string">&quot;ameameame&quot;</span></span><br><span class="line">	<span class="built_in">print</span>(name.count(<span class="string">&quot;a&quot;</span>))  <span class="comment"># 3 统计&quot;a&quot;出现的次数</span></span><br><span class="line">    <span class="built_in">print</span>(name.count(<span class="string">&quot;a&quot;</span>, <span class="number">0</span>, <span class="number">4</span>))  <span class="comment"># 2 在索引0-3 统计&quot;a&quot;出现的次数</span></span><br><span class="line">    </span><br><span class="line"><span class="number">12</span>、把字符串首字母都变成大写(可以用于标题)</span><br><span class="line">	res = <span class="string">&#x27;my name is ame nis IO KSn kls  &#x27;</span></span><br><span class="line">	<span class="built_in">print</span>(res.title())  <span class="comment"># 将所有英文单词首字母大写   My Name Is Ame Nis Io Ksn Kls </span></span><br><span class="line">	<span class="built_in">print</span>(res.capitalize())  <span class="comment"># 首字母大写 后面统一变小写    My name is ame nis io ksn kls</span></span><br><span class="line">    </span><br><span class="line"><span class="number">13</span>、判断字符串是否为纯数字</span><br><span class="line">	guess_age = <span class="built_in">input</span>(<span class="string">&#x27;请输入您的年龄&gt;&gt;&gt;:&#x27;</span>)</span><br><span class="line">    <span class="keyword">if</span> guess_age.isdigit():</span><br><span class="line">    	将字符串的数字转为整型的数字</span><br><span class="line">        guess_age = <span class="built_in">int</span>(guess_age)  </span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&#x27;请输入纯数字&#x27;</span>)</span><br><span class="line">        </span><br><span class="line"> <span class="number">14</span>、文本功能 替换关键字</span><br><span class="line">	res = <span class="string">&quot;my name is Ame Ame Ame&quot;</span></span><br><span class="line">    res.replace(<span class="string">&#x27;Ame&#x27;</span>, <span class="string">&#x27;yuki&#x27;</span>)  <span class="comment"># my name is yuki yuki yuki</span></span><br><span class="line">    res.</span><br><span class="line"> <span class="number">15</span>、列表转换成字符串 .join</span><br><span class="line">	<span class="built_in">list</span> = [<span class="string">&quot;1&quot;</span>, <span class="string">&quot;2&quot;</span>, <span class="string">&quot;3&quot;</span>, <span class="string">&quot;4&quot;</span>, <span class="string">&quot;5&quot;</span>, <span class="string">&quot;6&quot;</span>]</span><br><span class="line">	<span class="built_in">print</span>(<span class="string">&quot;,&quot;</span>.join(<span class="built_in">list</span>))  <span class="comment"># 1,2,3,4,5,6</span></span><br><span class="line">    <span class="comment"># 切记 只能转换列表是字符串你类型</span></span><br></pre></td></tr></table></figure>



<h3 id="1-3-列表"><a href="#1-3-列表" class="headerlink" title="1.3 列表"></a>1.3 列表</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1</span>、列表的查找(索引)</span><br><span class="line">	<span class="built_in">list</span> = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>]</span><br><span class="line">    <span class="built_in">list</span>[<span class="number">0</span>]  <span class="comment"># 1</span></span><br><span class="line">    <span class="built_in">list</span>[-<span class="number">1</span>]  <span class="comment"># 6</span></span><br><span class="line"><span class="number">2</span>、列表的查找(切片)</span><br><span class="line">	<span class="built_in">list</span> = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>]</span><br><span class="line">    <span class="built_in">list</span>[<span class="number">0</span>:<span class="number">5</span>]  <span class="comment"># 默认步长为一  [1, 2, 3, 4, 5] </span></span><br><span class="line">    <span class="built_in">list</span>[<span class="number">0</span>:<span class="number">5</span>:<span class="number">2</span>]  <span class="comment"># [1, 3, 5]</span></span><br><span class="line"><span class="number">3</span>、列表中元素的个数 <span class="built_in">len</span></span><br><span class="line">	<span class="built_in">list</span> = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>]</span><br><span class="line">    <span class="built_in">len</span>(<span class="built_in">list</span>)  <span class="comment"># 6</span></span><br><span class="line"><span class="number">4</span>、 列表的增删改查</span><br><span class="line">	<span class="built_in">list</span> = [<span class="string">&quot;ame&quot;</span>, <span class="string">&quot;yuki&quot;</span>, <span class="string">&quot;vlicecream&quot;</span>]</span><br><span class="line">    <span class="built_in">list</span>[<span class="number">0</span>] = <span class="string">&quot;yuki&quot;</span></span><br><span class="line"><span class="number">5</span>、列表增加元素 append 尾部追加</span><br><span class="line">	<span class="built_in">list</span> = [<span class="string">&quot;ame&quot;</span>, <span class="string">&quot;yuki&quot;</span>, <span class="string">&quot;vlicecream&quot;</span>]</span><br><span class="line">    <span class="built_in">list</span>.append[<span class="string">&quot;monica&quot;</span>]  <span class="comment"># list = [&quot;ame&quot;, &quot;yuki&quot;, &quot;vlicecream&quot;, &quot;monica&quot;]</span></span><br><span class="line"><span class="number">6</span>、列表删除元素 <span class="keyword">del</span> remove pop</span><br><span class="line">	<span class="comment"># del</span></span><br><span class="line">	<span class="built_in">list</span> = [<span class="string">&quot;ame&quot;</span>, <span class="string">&quot;yuki&quot;</span>, <span class="string">&quot;vlicecream&quot;</span>]</span><br><span class="line">    <span class="keyword">del</span> <span class="built_in">list</span>[<span class="number">0</span>]  <span class="comment"># list = [&quot;yuki&quot;, &quot;vlicecream&quot;]</span></span><br><span class="line">    <span class="comment"># remove  指名道姓的删除</span></span><br><span class="line">    <span class="built_in">list</span> = [<span class="string">&quot;ame&quot;</span>, <span class="string">&quot;yuki&quot;</span>, <span class="string">&quot;vlicecream&quot;</span>]</span><br><span class="line">    <span class="built_in">list</span>.remove(<span class="string">&quot;ame&quot;</span>)  <span class="comment"># list = [&quot;yuki&quot;, &quot;vlicecream&quot;]</span></span><br><span class="line">    <span class="comment"># pop</span></span><br><span class="line">    <span class="built_in">list</span> = [<span class="string">&quot;ame&quot;</span>, <span class="string">&quot;yuki&quot;</span>, <span class="string">&quot;vlicecream&quot;</span>]</span><br><span class="line">    <span class="built_in">list</span>.pop()  <span class="comment"># 如果没有加索引，那么是默认尾部弹出 也就是删除尾部，但是这个传出来的值是尾部的值   list = [&quot;ame&quot;, &quot;yuki&quot;]</span></span><br><span class="line">   </span><br><span class="line"><span class="number">7</span>、列表插入元素</span><br><span class="line">	<span class="built_in">list</span> = [<span class="string">&quot;ame&quot;</span>, <span class="string">&quot;yuki&quot;</span>, <span class="string">&quot;vlicecream&quot;</span>]</span><br><span class="line">    <span class="built_in">list</span>.insert(<span class="number">0</span>, <span class="string">&quot;monica&quot;</span>)  <span class="comment">#list = [&quot;monica&quot;, &quot;ame&quot;, &quot;yuki&quot;, &quot;vlicecream&quot;]</span></span><br><span class="line">    </span><br><span class="line"><span class="number">8</span>、拓展列表</span><br><span class="line">	<span class="built_in">list</span> = [<span class="string">&quot;ame&quot;</span>, <span class="string">&quot;yuki&quot;</span>, <span class="string">&quot;vlicecream&quot;</span>]</span><br><span class="line">    list1 = [<span class="string">&quot;am&quot;</span>, <span class="string">&quot;yuk&quot;</span>, <span class="string">&quot;vlicecrea&quot;</span>]</span><br><span class="line">    <span class="built_in">list</span>.extend(list1)</span><br><span class="line">    <span class="built_in">print</span>(<span class="built_in">list</span>)  <span class="comment"># [&#x27;ame&#x27;, &#x27;yuki&#x27;, &#x27;vlicecream&#x27;, &#x27;am&#x27;, &#x27;yuk&#x27;, &#x27;vlicecrea&#x27;]</span></span><br><span class="line">    </span><br><span class="line"><span class="number">9</span>、统计列表元素出现的个数</span><br><span class="line">    <span class="built_in">list</span> = [<span class="string">&quot;ame&quot;</span>, <span class="string">&quot;yuki&quot;</span>, <span class="string">&quot;vlicecream&quot;</span>]</span><br><span class="line">    <span class="built_in">print</span>(<span class="built_in">list</span>.count(<span class="string">&quot;yuki&quot;</span>))  <span class="comment"># 1 统计列表中某个元素出现的次数</span></span><br><span class="line"></span><br><span class="line"><span class="number">10</span>、清理列表</span><br><span class="line">    <span class="built_in">list</span>.clear()  <span class="comment"># 清空列表</span></span><br><span class="line">    <span class="built_in">print</span>(name_list)  <span class="comment"># list = []</span></span><br><span class="line"></span><br><span class="line"><span class="number">11</span>、列表的升序和降序</span><br><span class="line">	l2 = [<span class="number">11</span>,<span class="number">88</span>,<span class="number">22</span>,<span class="number">44</span>,<span class="number">33</span>,<span class="number">77</span>,<span class="number">66</span>,<span class="number">55</span>]</span><br><span class="line">    l2.sort()  <span class="comment"># 排序  默认是升序</span></span><br><span class="line">    <span class="built_in">print</span>(l2)  <span class="comment"># [11, 22, 33, 44, 55, 66, 77, 88]</span></span><br><span class="line">    l2.sort(reverse=<span class="literal">True</span>)  <span class="comment"># reverse=True 降序</span></span><br><span class="line">    <span class="built_in">print</span>(l2)  <span class="comment"># [88, 77, 66, 55, 44, 33, 22, 11]</span></span><br><span class="line"></span><br><span class="line">    l2.reverse()  <span class="comment"># 倒序</span></span><br><span class="line">    <span class="built_in">print</span>(l2)</span><br><span class="line"></span><br></pre></td></tr></table></figure>





<h3 id="1-4-字典"><a href="#1-4-字典" class="headerlink" title="1.4 字典"></a>1.4 字典</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 字典的查找(key)</span><br><span class="line">    dirt = &#123;<span class="string">&quot;name&quot;</span>: <span class="string">&quot;Ame&quot;</span>,</span><br><span class="line">            <span class="string">&quot;age&quot;</span>: <span class="number">18</span>,</span><br><span class="line">            <span class="string">&quot;hobby&quot;</span>: <span class="string">&quot;唱跳Rap篮球&quot;</span>&#125;</span><br><span class="line">    <span class="built_in">print</span>(dirt[<span class="string">&quot;name&quot;</span>])  <span class="comment"># Ame</span></span><br><span class="line">    <span class="comment"># 输入不存在的key 会报错</span></span><br><span class="line"><span class="number">2.</span> 字典keys:Value的个数(<span class="built_in">len</span>)</span><br><span class="line">    dirt = &#123;<span class="string">&quot;name&quot;</span>: <span class="string">&quot;Ame&quot;</span>,</span><br><span class="line">            <span class="string">&quot;age&quot;</span>: <span class="number">18</span>,</span><br><span class="line">            <span class="string">&quot;hobby&quot;</span>: <span class="string">&quot;唱跳Rap篮球&quot;</span>&#125;</span><br><span class="line">    <span class="built_in">print</span>(<span class="built_in">len</span>(dirt))  <span class="comment"># 3</span></span><br><span class="line"></span><br><span class="line"><span class="number">3.</span>字典的增改</span><br><span class="line">    dirt = &#123;<span class="string">&quot;name&quot;</span>: <span class="string">&quot;Ame&quot;</span>,</span><br><span class="line">            <span class="string">&quot;age&quot;</span>: <span class="number">18</span>,</span><br><span class="line">            <span class="string">&quot;hobby&quot;</span>: <span class="string">&quot;唱跳Rap篮球&quot;</span>&#125;</span><br><span class="line">    dirt[<span class="string">&quot;name&quot;</span>] = <span class="string">&quot;yuki&quot;</span></span><br><span class="line">    <span class="built_in">print</span>(dirt)  <span class="comment"># &#123;&#x27;name&#x27;: &#x27;yuki&#x27;, &#x27;age&#x27;: 18, &#x27;hobby&#x27;: &#x27;唱跳Rap篮球&#x27;&#125;</span></span><br><span class="line">    dirt[<span class="string">&quot;id_card&quot;</span>] = <span class="number">123456789</span></span><br><span class="line">    <span class="built_in">print</span>(dirt)  <span class="comment"># &#123;&#x27;name&#x27;: &#x27;yuki&#x27;, &#x27;age&#x27;: 18, &#x27;hobby&#x27;: &#x27;唱跳Rap篮球&#x27;, &#x27;id_card&#x27;: 123456789&#125;</span></span><br><span class="line"></span><br><span class="line"><span class="number">4.</span> 字典的成员运算 <span class="keyword">in</span>      <span class="keyword">not</span> <span class="keyword">in</span></span><br><span class="line">    dirt = &#123;<span class="string">&quot;name&quot;</span>: <span class="string">&quot;Ame&quot;</span>,</span><br><span class="line">            <span class="string">&quot;age&quot;</span>: <span class="number">18</span>,</span><br><span class="line">            <span class="string">&quot;hobby&quot;</span>: <span class="string">&quot;唱跳Rap篮球&quot;</span>&#125;</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;name&quot;</span> <span class="keyword">in</span> dirt)  <span class="comment"># True</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;hobby&quot;</span> <span class="keyword">not</span> <span class="keyword">in</span> dirt)  <span class="comment"># False</span></span><br><span class="line"></span><br><span class="line"><span class="number">5.</span> 字典的删除(<span class="keyword">del</span>   pop)</span><br><span class="line">	<span class="comment"># del</span></span><br><span class="line">	dirt = &#123;<span class="string">&quot;name&quot;</span>: <span class="string">&quot;Ame&quot;</span>,</span><br><span class="line">        <span class="string">&quot;age&quot;</span>: <span class="number">18</span>,</span><br><span class="line">        <span class="string">&quot;hobby&quot;</span>: <span class="string">&quot;唱跳Rap篮球&quot;</span>&#125;</span><br><span class="line">    <span class="keyword">del</span> dirt[<span class="string">&quot;name&quot;</span>]</span><br><span class="line">    <span class="built_in">print</span>(dirt)  <span class="comment"># &#123;&#x27;age&#x27;: 18, &#x27;hobby&#x27;: &#x27;唱跳Rap篮球&#x27;&#125;</span></span><br><span class="line">    <span class="comment"># pop</span></span><br><span class="line">    dirt = &#123;<span class="string">&quot;name&quot;</span>: <span class="string">&quot;Ame&quot;</span>,</span><br><span class="line">        <span class="string">&quot;age&quot;</span>: <span class="number">18</span>,</span><br><span class="line">        <span class="string">&quot;hobby&quot;</span>: <span class="string">&quot;唱跳Rap篮球&quot;</span>&#125;</span><br><span class="line">    dirt.pop(<span class="string">&quot;name&quot;</span>)</span><br><span class="line">    <span class="built_in">print</span>(dirt)  <span class="comment"># pop 则是指定key就去弹出，如果不指定 则默认尾部弹出</span></span><br><span class="line">    </span><br><span class="line"><span class="number">6.</span> 字典的keys values items</span><br><span class="line">	<span class="comment"># keys 显示出所有的key</span></span><br><span class="line">    dirt = &#123;<span class="string">&quot;name&quot;</span>: <span class="string">&quot;Ame&quot;</span>,</span><br><span class="line">            <span class="string">&quot;age&quot;</span>: <span class="number">18</span>,</span><br><span class="line">            <span class="string">&quot;hobby&quot;</span>: <span class="string">&quot;唱跳Rap篮球&quot;</span>&#125;  </span><br><span class="line">    <span class="built_in">print</span>(dirt.keys())  <span class="comment"># dict_keys([&#x27;name&#x27;, &#x27;age&#x27;, &#x27;hobby&#x27;])</span></span><br><span class="line">    <span class="comment"># values 显示出所有的value</span></span><br><span class="line">    dirt = &#123;<span class="string">&quot;name&quot;</span>: <span class="string">&quot;Ame&quot;</span>,</span><br><span class="line">        <span class="string">&quot;age&quot;</span>: <span class="number">18</span>,</span><br><span class="line">        <span class="string">&quot;hobby&quot;</span>: <span class="string">&quot;唱跳Rap篮球&quot;</span>&#125;</span><br><span class="line">	<span class="built_in">print</span>(dirt.values())  <span class="comment"># dict_values([&#x27;Ame&#x27;, 18, &#x27;唱跳Rap篮球&#x27;])</span></span><br><span class="line">    <span class="comment"># items 将items()的结果可以直接看成是列表[]套小列表()</span></span><br><span class="line">    dirt = &#123;<span class="string">&quot;name&quot;</span>: <span class="string">&quot;Ame&quot;</span>,</span><br><span class="line">            <span class="string">&quot;age&quot;</span>: <span class="number">18</span>,</span><br><span class="line">            <span class="string">&quot;hobby&quot;</span>: <span class="string">&quot;唱跳Rap篮球&quot;</span>&#125;</span><br><span class="line">    <span class="built_in">print</span>(dirt.items())  <span class="comment"># dict_items([(&#x27;name&#x27;, &#x27;Ame&#x27;), (&#x27;age&#x27;, 18), (&#x27;hobby&#x27;, &#x27;唱跳Rap篮球&#x27;)])</span></span><br><span class="line">    <span class="keyword">for</span> k, v <span class="keyword">in</span> dirt.items():  <span class="comment"># 解压赋值 结合 for循环</span></span><br><span class="line">        <span class="built_in">print</span>(k, v)  <span class="comment"># name Ame   age 18   hobby 唱跳Rap篮球</span></span><br><span class="line">        </span><br><span class="line"><span class="number">7.</span>字典的循环</span><br><span class="line">    dirt = &#123;<span class="string">&quot;name&quot;</span>: <span class="string">&quot;Ame&quot;</span>,</span><br><span class="line">            <span class="string">&quot;age&quot;</span>: <span class="number">18</span>,</span><br><span class="line">            <span class="string">&quot;hobby&quot;</span>: <span class="string">&quot;唱跳Rap篮球&quot;</span>&#125;</span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> dirt:</span><br><span class="line">        <span class="built_in">print</span>(i)  <span class="comment"># name  age   hobby</span></span><br><span class="line">  </span><br><span class="line"><span class="number">8.</span>字典的推荐使用查找方法(get)</span><br><span class="line">	dirt = &#123;<span class="string">&quot;name&quot;</span>: <span class="string">&quot;Ame&quot;</span>,</span><br><span class="line">        <span class="string">&quot;age&quot;</span>: <span class="number">18</span>,</span><br><span class="line">        <span class="string">&quot;hobby&quot;</span>: <span class="string">&quot;唱跳Rap篮球&quot;</span>&#125;</span><br><span class="line">	<span class="built_in">print</span>(dirt.get(<span class="string">&quot;name&quot;</span>))  <span class="comment"># Ame</span></span><br><span class="line">    dirt = &#123;<span class="string">&quot;name&quot;</span>: <span class="string">&quot;Ame&quot;</span>,</span><br><span class="line">        <span class="string">&quot;age&quot;</span>: <span class="number">18</span>,</span><br><span class="line">        <span class="string">&quot;hobby&quot;</span>: <span class="string">&quot;唱跳Rap篮球&quot;</span>&#125;</span><br><span class="line">    <span class="built_in">print</span>(dirt.get(<span class="string">&quot;id card&quot;</span>))  <span class="comment"># None</span></span><br><span class="line">    <span class="built_in">print</span>(dirt.get(<span class="string">&quot;id card&quot;</span>, <span class="number">123456</span>))  <span class="comment"># 123456</span></span><br><span class="line">    dirt.get(<span class="string">&quot;name&quot;</span>, <span class="string">&quot;yuki&quot;</span>)  <span class="comment"># Ame</span></span><br><span class="line">    <span class="built_in">print</span>(dirt)  <span class="comment"># &#123;&#x27;name&#x27;: &#x27;Ame&#x27;, &#x27;age&#x27;: 18, &#x27;hobby&#x27;: &#x27;唱跳Rap篮球&#x27;&#125;</span></span><br><span class="line">    <span class="comment"># get 假如输入没有的key是不会报错的，会返回None ，所以推荐用get</span></span><br><span class="line">    </span><br><span class="line"><span class="number">9.</span> 字典的创建方法</span><br><span class="line">	I   dirt = &#123;<span class="string">&#x27;name&#x27;</span>: <span class="string">&#x27;Ame&#x27;</span>, <span class="string">&#x27;age&#x27;</span>: <span class="number">18</span>, <span class="string">&#x27;hobby&#x27;</span>: <span class="string">&#x27;唱跳Rap篮球&#x27;</span>&#125;</span><br><span class="line">    II  dirt = &#123;key1:value1, key2:value2&#125;</span><br><span class="line">    III <span class="built_in">list</span> = [</span><br><span class="line">        [<span class="string">&#x27;username&#x27;</span>,<span class="string">&#x27;jason&#x27;</span>],</span><br><span class="line">        [<span class="string">&#x27;password&#x27;</span>,<span class="number">123</span>],</span><br><span class="line">        [<span class="string">&#x27;hobby&#x27;</span>,[<span class="string">&#x27;read&#x27;</span>,<span class="string">&#x27;run&#x27;</span>]]</span><br><span class="line">        ]</span><br><span class="line">        dirt = &#123;&#125;</span><br><span class="line">        <span class="keyword">for</span> k, v <span class="keyword">in</span> <span class="built_in">list</span>:</span><br><span class="line">            dirt[k] = v</span><br><span class="line">        <span class="built_in">print</span>(dirt)</span><br><span class="line">        <span class="comment"># 列表转字典 方法，一定要注意</span></span><br><span class="line">        </span><br><span class="line"> <span class="number">10.</span> 字典的清空(clear)</span><br><span class="line">	dirt = &#123;<span class="string">&#x27;name&#x27;</span>: <span class="string">&#x27;Ame&#x27;</span>, <span class="string">&#x27;age&#x27;</span>: <span class="number">18</span>, <span class="string">&#x27;hobby&#x27;</span>: <span class="string">&#x27;唱跳Rap篮球&#x27;</span>&#125;</span><br><span class="line">    dirt.clear()</span><br><span class="line">    <span class="built_in">print</span>(dirt)  <span class="comment"># &#123;&#125;</span></span><br><span class="line">    </span><br><span class="line"><span class="number">11.</span> 快速生成字典(fromkeys)</span><br><span class="line">	<span class="built_in">dict</span>.fromkeys()  <span class="comment"># 快速先生成一个字典</span></span><br><span class="line">    <span class="built_in">list</span> = [<span class="string">&#x27;username&#x27;</span>,<span class="string">&#x27;pwd&#x27;</span>,<span class="string">&#x27;hobby&#x27;</span>]</span><br><span class="line">    res = <span class="built_in">dict</span>.fromkeys(l,<span class="literal">None</span>)</span><br><span class="line">	<span class="built_in">print</span>(res)</span><br><span class="line">    </span><br><span class="line"><span class="number">12.</span> 字典删除键值对(popitem)</span><br><span class="line">	dirt = &#123;<span class="string">&#x27;name&#x27;</span>: <span class="string">&#x27;Ame&#x27;</span>, <span class="string">&#x27;age&#x27;</span>: <span class="number">18</span>, <span class="string">&#x27;hobby&#x27;</span>: <span class="string">&#x27;唱跳Rap篮球&#x27;</span>&#125;</span><br><span class="line">    dirt.popitem()</span><br><span class="line">    <span class="built_in">print</span>(dirt)  <span class="comment"># 字典弹出键值对 尾部弹出</span></span><br><span class="line">    </span><br><span class="line"><span class="number">13.</span> 字典的修改(setdefault, update)</span><br><span class="line">	<span class="comment"># setdefault</span></span><br><span class="line">	<span class="built_in">dict</span>.setdefault()  重点</span><br><span class="line">    dd = &#123;<span class="string">&#x27;name&#x27;</span>:<span class="string">&#x27;jason&#x27;</span>,<span class="string">&#x27;pwd&#x27;</span>:<span class="number">123</span>&#125;</span><br><span class="line">    dd.setdefault(<span class="string">&#x27;age&#x27;</span>,<span class="number">18</span>)  <span class="comment"># 键不存在 则新建</span></span><br><span class="line">    res = dd.setdefault(<span class="string">&#x27;name&#x27;</span>,<span class="string">&#x27;egonSBSBSB&#x27;</span>)  <span class="comment"># 键存在不修改 而是获取到键对应的值</span></span><br><span class="line">    <span class="built_in">print</span>(dd,res)  <span class="comment"># &#123;&#x27;name&#x27;: &#x27;jason&#x27;, &#x27;pwd&#x27;: 123, &#x27;age&#x27;: 18&#125; jason</span></span><br><span class="line">	<span class="comment"># update</span></span><br><span class="line">    <span class="built_in">dict</span>.update()</span><br><span class="line">    dd = &#123;<span class="string">&#x27;name&#x27;</span>:<span class="string">&#x27;jason&#x27;</span>,<span class="string">&#x27;pwd&#x27;</span>:<span class="number">123</span>&#125;</span><br><span class="line">    dd.update(&#123;<span class="string">&#x27;age&#x27;</span>:<span class="number">18</span>&#125;)  <span class="comment"># 键不存在 则新建</span></span><br><span class="line">    dd.update(&#123;<span class="string">&#x27;name&#x27;</span>:<span class="string">&#x27;EGONDSB&#x27;</span>&#125;)  <span class="comment"># 键存在则修改</span></span><br><span class="line">    <span class="built_in">print</span>(dd)</span><br><span class="line">    <span class="comment"># 还不如使用下列方式</span></span><br><span class="line">    dd[<span class="string">&#x27;age&#x27;</span>] = <span class="number">18</span></span><br><span class="line">    dd[<span class="string">&#x27;name&#x27;</span>] = <span class="string">&#x27;jason&#x27;</span></span><br></pre></td></tr></table></figure>

<h3 id="1-5-元组"><a href="#1-5-元组" class="headerlink" title="1.5 元组"></a>1.5 元组</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 什么是元组</span><br><span class="line">	小括号括起来，里面可以装N多元素的，以逗号隔开，元素可以是任意类型，但是元组是不可变类型，要注意元组只有一个元素时，一定要有逗号，所以以后凡是容器类型，只有一个元素时都不能省略逗号</span><br><span class="line"><span class="number">2.</span> 按索引取值</span><br><span class="line">	tup = (<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>)</span><br><span class="line">    <span class="built_in">print</span>(tup[<span class="number">0</span>])  <span class="comment"># 1</span></span><br><span class="line">    <span class="built_in">print</span>(tup[-<span class="number">1</span>])  <span class="comment"># 5</span></span><br><span class="line"><span class="number">3.</span>	<span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">	笔试题</span></span><br><span class="line"><span class="string">    元组内元素的内存地址不能修改</span></span><br><span class="line"><span class="string">    但是如果元素是可变类型 那么元素值可以改</span></span><br><span class="line"><span class="string">	&quot;&quot;&quot;</span></span><br><span class="line"><span class="number">4.</span> 切片</span><br><span class="line">	跟列表一致</span><br><span class="line">   </span><br><span class="line"><span class="number">5.</span> 长度(<span class="built_in">len</span>)</span><br><span class="line">	跟列表一致</span><br><span class="line">    </span><br><span class="line"><span class="number">6.</span> 成员运算(<span class="keyword">in</span>   <span class="keyword">not</span> <span class="keyword">in</span>)</span><br><span class="line">	跟列表一致</span><br><span class="line">    </span><br><span class="line"><span class="number">7.</span> 循环</span><br><span class="line">	tup = (<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>)</span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> tup</span><br><span class="line">    	<span class="built_in">print</span>(i)</span><br></pre></td></tr></table></figure>

<h3 id="1-6-集合"><a href="#1-6-集合" class="headerlink" title="1.6 集合"></a>1.6 集合</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>什么是集合</span><br><span class="line">	大括号括起来 内部可以存放多个元素</span><br><span class="line">	元素与元素之间逗号隔开  元素只能是不可变类型(整型 浮点型 字符串 元组)</span><br><span class="line">    </span><br><span class="line"><span class="number">2.</span>集合的应用场景</span><br><span class="line">	只有两种情况才会考虑使用集合</span><br><span class="line">        <span class="number">1.</span>关系运算</span><br><span class="line">            两个群体之间判断 交叉并集</span><br><span class="line">                共同好友 共同关注 共同点赞...</span><br><span class="line">        <span class="number">2.</span>去重操作</span><br><span class="line">            去除重复的元素</span><br><span class="line">            </span><br><span class="line"><span class="number">3.</span> 集合的定义</span><br><span class="line">	<span class="comment"># 因为跟字典很像，所以定义空&#123;&#125;的时候 默认是空字典 所以需要加set来声明这是集合</span></span><br><span class="line">    </span><br><span class="line"><span class="number">4.</span> 集合的去重操作</span><br><span class="line">	<span class="comment"># 去重不保留顺序</span></span><br><span class="line">	l = [<span class="number">11</span>,<span class="number">22</span>,<span class="number">33</span>,<span class="number">22</span>,<span class="number">22</span>,<span class="number">33</span>,<span class="number">11</span>,<span class="number">22</span>,<span class="number">55</span>,<span class="number">66</span>,<span class="number">66</span>,<span class="number">77</span>,<span class="number">77</span>]</span><br><span class="line">    <span class="built_in">set</span> = <span class="built_in">set</span>()</span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> l:</span><br><span class="line">        <span class="keyword">if</span> i <span class="keyword">not</span> <span class="keyword">in</span> <span class="built_in">set</span>:</span><br><span class="line">            <span class="built_in">set</span>.add(i)</span><br><span class="line">    <span class="built_in">print</span>(<span class="built_in">set</span>)</span><br><span class="line">	</span><br><span class="line">    </span><br><span class="line">    <span class="comment"># 去重保留顺序</span></span><br><span class="line">	l = [<span class="number">11</span>,<span class="number">22</span>,<span class="number">33</span>,<span class="number">22</span>,<span class="number">22</span>,<span class="number">33</span>,<span class="number">11</span>,<span class="number">22</span>,<span class="number">55</span>,<span class="number">66</span>,<span class="number">66</span>,<span class="number">77</span>,<span class="number">77</span>]</span><br><span class="line">    new_l = []</span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> l:</span><br><span class="line">        <span class="keyword">if</span> i <span class="keyword">not</span> <span class="keyword">in</span> new_l:</span><br><span class="line">            new_l.append(i)</span><br><span class="line">    <span class="built_in">print</span>(new_l)</span><br><span class="line"></span><br><span class="line"><span class="number">5.</span> 关系运算</span><br><span class="line">	f1 = &#123;<span class="string">&#x27;jason&#x27;</span>,<span class="string">&#x27;egon&#x27;</span>,<span class="string">&#x27;kevin&#x27;</span>,<span class="string">&#x27;tony&#x27;</span>&#125;</span><br><span class="line">	f2 = &#123;<span class="string">&#x27;jason&#x27;</span>,<span class="string">&#x27;jack&#x27;</span>,<span class="string">&#x27;oscar&#x27;</span>,<span class="string">&#x27;kevin&#x27;</span>&#125;</span><br><span class="line">I 共同好友</span><br><span class="line">	f1 &amp; f2</span><br><span class="line">II f1独有的好友</span><br><span class="line">	f1 - f2</span><br><span class="line">III f2独有的好友</span><br><span class="line">	f2 - f1</span><br><span class="line">IV 所有的好友</span><br><span class="line">	f1 | f2</span><br><span class="line">V  各自的好友 不包括共同好友</span><br><span class="line">	f1 ^ f2</span><br><span class="line"><span class="number">6.</span> 父集 子集</span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">父级:A包含B 那么A就是B的父级</span></span><br><span class="line"><span class="string">子级:A包含B 那么B就是A的子级</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line">s1 = &#123;<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>&#125;</span><br><span class="line">s2 = &#123;<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>&#125;</span><br><span class="line"><span class="built_in">print</span>(s1 &gt; s2)  <span class="comment"># True</span></span><br><span class="line"><span class="built_in">print</span>(s1 &lt; s2)  <span class="comment"># False</span></span><br></pre></td></tr></table></figure>



<h2 id="二、可变类型与不可变类型"><a href="#二、可变类型与不可变类型" class="headerlink" title="二、可变类型与不可变类型"></a>二、可变类型与不可变类型</h2><h3 id="1-1-可变类型"><a href="#1-1-可变类型" class="headerlink" title="1.1 可变类型"></a>1.1 可变类型</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">可变类型</span></span><br><span class="line"><span class="string">    值改变 内存地址不变</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="comment"># 可变类型</span></span><br><span class="line">l1 = [<span class="number">11</span>,<span class="number">22</span>]</span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">id</span>(l1))  <span class="comment"># 2133952624128</span></span><br><span class="line">l1.append(<span class="number">666</span>)</span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">id</span>(l1))  <span class="comment"># 2133952624128</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="1-2-不可变类型"><a href="#1-2-不可变类型" class="headerlink" title="1.2 不可变类型"></a>1.2 不可变类型</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">不可变类型</span></span><br><span class="line"><span class="string">    值改变 内存地址一定变</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="comment"># 不可变类型</span></span><br><span class="line">    a = <span class="number">1</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="built_in">id</span>(a))  </span><br><span class="line">    a = <span class="number">2</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="built_in">id</span>(a))  </span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>


            </div>

            <!-- Post Comments -->
            
    <!-- 使用 DISQUS_CLICK -->
<div id="disqus-comment">
    <div id="disqus_thread"></div>

<!-- add animation -->
<style>
	.disqus_click_btn {
            line-height: 30px;
            margin: 0;
            min-width: 50px;
            padding: 0 14px;
            display: inline-block;
            font-family: "Roboto", "Helvetica", "Arial", sans-serif;
            font-size: 14px;
            font-weight: 400;
            text-transform: uppercase;
            letter-spacing: 0;
            overflow: hidden;
            will-change: box-shadow;
            transition: box-shadow .2s cubic-bezier(.4, 0, 1, 1), background-color .2s cubic-bezier(.4, 0, .2, 1), color .2s cubic-bezier(.4, 0, .2, 1);
            outline: 0;
            cursor: pointer;
            text-decoration: none;
            text-align: center;
            vertical-align: middle;
            border: 0;
            background: rgba(158, 158, 158, .2);
            box-shadow: 0 2px 2px 0 rgba(0, 0, 0, .14), 0 3px 1px -2px rgba(0, 0, 0, .2), 0 1px 5px 0 rgba(0, 0, 0, .12);
            color: #fff;
            background-color: #7EC0EE;
            text-shadow: 0
        }
</style>
	
<div class="btn_click_load" id="disqus_bt"> 
    <button class="disqus_click_btn">点击查看评论</button>
</div>

<!--
<script type="text/javascript">
$('.btn_click_load').click(function() {
    /* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
    var disqus_shortname = 'http-miccall-tech'; // required: replace example with your forum shortname

    /* * * DON'T EDIT BELOW THIS LINE * * */
    (function() {
      var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
      dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
      (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
    })();

    document.getElementById('disqus_bt').style.display = "none";
});
</script>
-->
<script type="text/javascript">
    var disqus_config = function () {
        this.page.url = 'https://gitee.com/AmeX/amelin-ting.git/2017/03/28/Python------%E5%9F%BA%E6%9C%AC%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%E5%8F%8A%E5%86%85%E7%BD%AE%E6%96%B9%E6%B3%95/';  // Replace PAGE_URL with your page's canonical URL variable
        this.page.identifier = 'https://gitee.com/AmeX/amelin-ting.git/2017/03/28/Python------%E5%9F%BA%E6%9C%AC%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%E5%8F%8A%E5%86%85%E7%BD%AE%E6%96%B9%E6%B3%95/'; // Replace PAGE_IDENTIFIER with your page's unique identifier variable
    };
</script>

<script type="text/javascript">
    $('.btn_click_load').click(function() {  //click to load comments
        (function() { // DON'T EDIT BELOW THIS LINE
            var d = document;
            var s = d.createElement('script');
            s.src = '//http-miccall-tech.disqus.com/embed.js';
            s.setAttribute('data-timestamp', + new Date());
            (d.head || d.body).appendChild(s);
        })();
        $('.btn_click_load').css('display','none');
    });
</script>
</div>
<style>
    #disqus-comment{
        background-color: #eee;
        padding: 2pc;
    }
</style>


        </div>
        <!-- Copyright 版权 start -->
                <div id="copyright">
            <ul>
                <li>&copy;Powered By <a target="_blank" rel="noopener" href="https://hexo.io/zh-cn/" style="border-bottom: none;">hexo</a></li>
                <li>Design: <a target="_blank" rel="noopener" href="http://miccall.tech " style="border-bottom: none;">miccall</a></li>
            </ul>
            
                <span id="busuanzi_container_site_pv">本站总访问量<span id="busuanzi_value_site_pv"></span>次</span>
			
        </div>
    </div>
</body>



 	
</html>
